package com.flymobi.service;

import java.util.ArrayList;
import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.fw.util.StringUtil;
import com.flymobi.po.Res;
import com.flymobi.util.QL;
import com.flymobi.vo.ResSearchVo;

public class ResService extends CommonService {
	
	public List<Res> list(ResSearchVo param,int startRow, int pagePerSize) {
		String hql="from Res m where 1=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getStatus()!=null&&param.getStatus()!=-1){
				hql+=" and m.status =?";
				paramList.add(param.getStatus());
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
			if(!StringUtil.isEmpty(param.getKey())&&param.getKey().trim().length()>0){
				hql+=" and (m.title like ? or m.tag like ?)";
				paramList.add("%"+param.getKey()+"%");
				paramList.add("%"+param.getKey()+"%");
			}
			if(param.getManagerId()!=null&&param.getManagerId()>0){
				hql+=" and m.managerId=?";
				paramList.add(param.getManagerId());
			}
			if(param.getClsId()!=null&&param.getClsId()>0){
				hql+=" and (m.clsId=? or m.clsId in(select n.id from Cls n where n.fatherId=?))";
				paramList.add(param.getClsId());
				paramList.add(param.getClsId());
			}
			if(param.getType()!=null&&!param.getType().equals("-1")){
				hql+=" and m.type=?";
				paramList.add(param.getType());
			}
		}
		hql+=" order by m.id desc";
		return dao.find(Res.class,hql, paramList,startRow, pagePerSize);
	}
	
	public Long getCount(ResSearchVo param) {
		String hql="select count(*) from Res m where 1=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getStatus()!=null&&param.getStatus()!=-1){
				hql+=" and m.status =?";
				paramList.add(param.getStatus());
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?"; 
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
			if(param.getKey()!=null&&param.getKey().trim().length()>0){
				hql+=" and (m.title like ? or m.tag like ?)";
				paramList.add("%"+param.getKey()+"%");
				paramList.add("%"+param.getKey()+"%");
			}
			if(param.getManagerId()!=null&&param.getManagerId()>0){
				hql+=" and m.managerId=?";
				paramList.add(param.getManagerId());
			}
			if(param.getClsId()!=null&&param.getClsId()>0){
				hql+=" and (m.clsId=? or m.clsId in(select n.id from Cls n where n.fatherId=?))";
				paramList.add(param.getClsId());
				paramList.add(param.getClsId());
			}
			if(param.getType()!=null&&!param.getType().equals("-1")){
				hql+=" and m.type=?";
				paramList.add(param.getType());
			}
		}
		return dao.getCount(hql, paramList);
	}

	public void save(Res News) {
		dao.save(News);
	}

	public Res load(String id) {
		return dao.load(Res.class, new Long(id));
	}
	
	public Res get(String id) {
		return dao.get(Res.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}

	/**
	 * @param id
	 * @return
	 */
	public Res getPrev(Long id,Long pindaoId) {
		return dao.findSingle(Res.class, QL.HQL.Res.Get_Prev, new Object[]{id,pindaoId});
	}

	/**
	 * @param id
	 * @return
	 */
	public Res getNext(Long id,Long pindaoId) {
		return dao.findSingle(Res.class, QL.HQL.Res.Get_Next, new Object[]{id,pindaoId});
	}
}
